let Api = '/shopStreet'

export const HTTP = (opts) => {
	return new Promise((resolve, reject) => {
		let isLoading = opts.isLoading || true
		
		let token = null;
		let loginInfo = JSON.parse(localStorage.getItem('loginInfo'))
		if(loginInfo && loginInfo.token){
			token = loginInfo.token
		}
		
		if(isLoading) {
			uni.showLoading()
		}
		let contentType = 'application/x-www-form-urlencoded'
		if (opts.dataType === 'json') {
		  contentType = 'application/json'
		}
		if (opts.dataType === 'file') {
		  contentType = 'multipart/form-data'
		}
		uni.request({
		    url: Api+opts.url,
		    data: opts.data || {},
		    header: {
				'Content-Type': contentType,
				'ssToken': token
			},
			method: opts.method || 'POST',
			dataType: opts.dataType || 'json',
		    success: (res) => {
				if(res.data.status > 0){
					uni.showToast({
					    title: res.data.message,
					    duration: 2000,
						icon: 'none'
					});
				}
				if(res.data.code === 704){
					uni.showToast({
					    title: "请登录",
					    duration: 2000,
						icon: 'none'
					});
					setTimeout(()=>{
						uni.hideToast();
						uni.reLaunch({
						    url: '/pages/user/login'
						});
					},2000)
				}
				if(!res.data.flag && res.data.code !== 704){
					uni.showToast({
					    title: res.data.msg,
					    duration: 2000,
						icon: 'none'
					});
					setTimeout(()=>{
						uni.hideToast();
					},2000)
				}
				resolve(res.data)
		    },
			fail: (err) => {
				reject(err)
			},
			complete: (comp) => {
				if(isLoading) {
					uni.hideLoading()
				}
			}
		});
	})
}
